package cn.darkhorse.basic.service.impl;

import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.darkhorse.basic.mapper.QmsSampleCarMapper;
import cn.darkhorse.basic.domain.QmsSampleCar;
import cn.darkhorse.basic.service.IQmsSampleCarService;

/**
 * 样品车管理Service业务层处理
 *
 * @author ytq
 * @date 2024-07-18
 */
@Service
public class QmsSampleCarServiceImpl extends ServiceImpl<QmsSampleCarMapper, QmsSampleCar> implements IQmsSampleCarService
{
    @Autowired
    private QmsSampleCarMapper qmsSampleCarMapper;

    /**
     * 查询样品车管理
     *
     * @param id 样品车管理主键
     * @return 样品车管理
     */
    @Override
    public QmsSampleCar selectQmsSampleCarById(Long id)
    {
        return qmsSampleCarMapper.selectQmsSampleCarById(id);
    }

    /**
     * 查询样品车管理列表
     *
     * @param qmsSampleCar 样品车管理
     * @return 样品车管理
     */
    @Override
    public List<QmsSampleCar> selectQmsSampleCarList(QmsSampleCar qmsSampleCar)
    {
        return qmsSampleCarMapper.selectQmsSampleCarList(qmsSampleCar);
    }

    /**
     * 新增样品车管理
     *
     * @param qmsSampleCar 样品车管理
     * @return 结果
     */
    @Override
    public int saveQmsSampleCar(QmsSampleCar qmsSampleCar) {
        if (qmsSampleCar.getId() == null){
            Long count = lambdaQuery().eq(QmsSampleCar::getSampleCarNumber, qmsSampleCar.getSampleCarNumber()).count();
            if (count > 0){
                throw new RuntimeException("车号重复");
            }
            qmsSampleCar.setCreateTime(DateUtils.getNowDate());

            return qmsSampleCarMapper.insertQmsSampleCar(qmsSampleCar);
        }else{
            Long count = lambdaQuery().eq(QmsSampleCar::getSampleCarNumber, qmsSampleCar.getSampleCarNumber()).ne(QmsSampleCar::getId, qmsSampleCar.getId()).count();
            if (count > 0){
                throw new RuntimeException("车号重复");
            }
            return qmsSampleCarMapper.updateQmsSampleCar(qmsSampleCar);
        }
    }


    /**
     * 批量删除样品车管理
     *
     * @param ids 需要删除的样品车管理主键
     * @return 结果
     */
    @Override
    public int deleteQmsSampleCarByIds(Long[] ids)
    {
        return qmsSampleCarMapper.deleteQmsSampleCarByIds(ids);
    }

    /**
     * 删除样品车管理信息
     *
     * @param id 样品车管理主键
     * @return 结果
     */
    @Override
    public int deleteQmsSampleCarById(Long id)
    {
        return qmsSampleCarMapper.deleteQmsSampleCarById(id);
    }
}
